$(document).ready(function(){
    $('input[id^="choose_address_"]').click(function(){
        $("#delivery-address").css("display","none");
    });
});

function show_address_form(){
    $("#delivery-address").css("display","block");
}
function add_address() {
    var consignee = $("#id_consignee").val();
    var street_address = $("#id_street_address").val();
    var postal_code = $("#id_postal_code").val();
    var city = $("#id_city").val();
    if ($("#id_state").val()!='') {
        var state = $("#id_state").val();
    }
    var country = $("#id_country").val();
    var phone = $("#id_phone").val();
    var token = $("#csrfmiddlewaretoken").val();
    var submit = "add"
    if ($("#address_id").val()!='') {
        var address_id = $("#address_id").val();
        data = {
            'consignee':consignee,
            'street_address': street_address,
            'postal_code': postal_code,
            'city': city,
            'country': country,
            'phone': phone,
            'address_id': address_id,
            'submit': submit,
            "csrfmiddlewaretoken": token
        }
    }else {
        data = {
            'consignee':consignee,
            'street_address': street_address,
            'postal_code': postal_code,
            'city': city,
            'country': country,
            'phone': phone,
            'submit': submit,
            "csrfmiddlewaretoken": token
        }
    }
    url = "/user_panel/account_management/delivery_address/"
    $.post(url, data, function(result){
        if (result['success']==true) {
            document.getElementById("delivery-address-form").reset();
            $("#address_id").val('');
            if(document.getElementById("my-address-"+data['address_id'])==undefined){
                if (state!=undefined) {
                    address_string = consignee+", "+street_address+", "+postal_code+", "+city+", "+state+", "+country+", "+phone;
                }
                else {
                    address_string = consignee+", "+street_address+", "+postal_code+", "+city+", "+country+", "+phone;
                }
                $("#my-delivery-address p:first").after('<p id="my-address-'+result['address_id']+'">'
                            + '<input type="radio" value="'+result['address_id']+'" name="choose-address" id="choose_address_'+result['address_id']+'" />'
                            +'"'+address_string+'"'
                            +'&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<a href="javascript:void(0)" onclick="modify_address({{my_address.id}})" style="color:blue;">[Modify]</a>'
                            +'</p>');
                $("#choose_address_"+result['address_id']).click(function(){
                    $("#delivery-address").css("display","none");
                });
            }
            else {
                if (state!=undefined) {
                    address_string = consignee+", "+street_address+", "+postal_code+", "+city+", "+state+", "+country+", "+phone;
                }
                else {
                    address_string = consignee+", "+street_address+", "+postal_code+", "+city+", "+country+", "+phone;
                }
                $("#my-address-"+data['address_id']).html('<input type="radio" value="'+result['address_id']+'" name="choose-address" id="choose_address_'+result['address_id']+'" />'
                            +'"'+address_string+'"'
                            +'&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<a href="javascript:void(0)" onclick="modify_address({{my_address.id}})" style="color:blue;">[Modify]</a>'
                            +'</p>');
            }
        }
        else if (result['success']==false) {
            alert("The address has already been saved!");
        }
    });    
}

function modify_address(address_id) {
    $("#delivery-address").css("display","block");
    var submit = "modify"
    url = "/user_panel/account_management/delivery_address/"
    $.post(url, {'address_id':address_id, 'submit':submit}, function(data){
        if (data['success']==true) {
            $("#address_id").val(data['address_id']);
            $("#id_consignee").val(data['consignee']);
            $("#id_street_address").val(data['street_address']);
            $("#id_postal_code").val(data['postal_code']);
            $("#id_city").val(data['city']);
            $("#id_state").val(data['state']);
            $("#id_country").val(data['country']);
            $("#id_phone").val(data['phone']);
        }
    });
}

function jump_to_check_out_page() {
    value = $('input[name="choose-address"]:checked').val();
    if(value!='' && value!=undefined) {
        url = "/cart/confirmation/"+value+"/";
        window.location.href = url;
    }
    else {
        alert("Please choose your delivery address or add a new one!");
    }
}